<?php
namespace core\models;
/**
 * 生成验证码图片类
 * LICENSE
 * @category   Verification Code
 * @package    扩展库
 * @subpackage 验证码
 * @version    $Id: verification_code.php 2011-12-26
 */
class Widget_VerifyCode {
    /**
     * 生成验证码的长度
     * @var int
     */
    private $length = 5;

    /**
     * Enter description here ...
     * @var unknown_type
     */
    private $themes_jpg = array(
        1  => array('captcha_bg1.jpg', 255, 255, 255),
        2  => array('captcha_bg2.jpg', 0, 0, 0),
        3  => array('captcha_bg3.jpg', 0, 0, 0),
        4  => array('captcha_bg4.jpg', 255, 255, 255),
        5  => array('captcha_bg5.jpg', 255, 255, 255),
        6  => array('captcha_bg6.jpg', 238, 92, 66),
        7  => array('captcha_bg7.jpg', 139, 0, 0),
        8  => array('captcha_bg8.jpg', 139, 0, 0),
        9  => array('captcha_bg9.jpg', 36, 223, 200),
        10 => array('captcha_bg10.jpg', 0, 123, 200),
    );

    /**
     * Enter description here ...
     * @var unknown_type
     */
    private $themes_gif = array(
        1  => array('captcha_bg1.gif', 255, 255, 255),
        2  => array('captcha_bg2.gif', 0, 0, 0),
        3  => array('captcha_bg3.gif', 0, 0, 0),
        4  => array('captcha_bg4.gif', 255, 255, 255),
        5  => array('captcha_bg5.gif', 255, 255, 255),
        6  => array('captcha_bg6.gif', 34, 139, 34),
        7  => array('captcha_bg7.gif', 160, 32, 240),
        8  => array('captcha_bg8.gif', 160, 32, 240),
        9  => array('captcha_bg9.gif', 0, 205, 0),
        10 => array('captcha_bg9.gif', 0, 5, 255),
    );

    /**
     * 图片的宽度
     * @var integer $width
     */
    private $width = 130;

    /**
     * 图片的高度
     * @var integer $height
     */
    private $height = 20;

    /**
     * 背景图片所在目录
     * @var string $folder
     */
    private $folder = '';

    /**
     * 图片的文件类型
     * @var string $img_type
     */
    private $img_type = 'png';

    /**
     * 存在SESSION中的验证码名称
     * @var string
     */
    private $session_name = 'verify_code';

    /**
     * 生成验证码函数，如果要显示验证码，可以用该类的静态方法echo_gc_image
     * @param int    $len          出现的验证位数
     * @param string $session_name 保存验证码的SESSION名称
     * @author walter,lwkai
     */
    public function __construct($len = 5, $session_name = 'verify_code') {
        $this->length       = ((int) $len ? (int) $len : 5);
        $this->session_name = $session_name;
        $this->folder       = Z_DIR . '/image/Capt/';
    }

    /**
     * 取得验证码的值
     * @return string 返回值
     */
    public function getVerifyCode(){
        return $_SESSION[$this->session_name];
    }
    public function set_width($width) {
        if ((int) $width) {
            $this->width = (int) $width;
        }
    }

    public function set_height($height) {
        if ((int) $height) {
            $this->height = $height;
        }
    }

    /**
     * 生成随机字符串验证码字符
     * @return string $hash (生成的字符串)
     * @author lwkai 2012-09-07
     */
    private function rand_str() {
        $hash  = '';
        $chars = 'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789abcdefghijklmnopqrstuvwxyz';
        $max   = strlen($chars) - 1;
        for ($i = 0; $i < $this->length; $i++) {
            $hash .= $chars[mt_rand(0, $max)];
        }
        $_SESSION[$this->session_name] = $hash;
        return $hash;
    }

    /**
     * 输出验证码图片
     */
    public function output_image($word = '') {
        if ($word == '') {
            $word = $this->rand_str();
            $word = strtoupper($word);
        }
        if (function_exists('imagecreatefromjpeg') && ((imagetypes() & IMG_JPG) > 0)) {
            $theme = $this->themes_jpg[mt_rand(1, count($this->themes_jpg))];
        } else {
            $theme = $this->themes_gif[mt_rand(1, count($this->themes_gif))];
        }

        if (!file_exists($this->folder . $theme[0])) {
            throw \Z::Error('图片没找到！');
            return false;
        } else {
            $img_bg    = (function_exists('imagecreatefromjpeg') && ((imagetypes() & IMG_JPG) > 0)) ?
                imagecreatefromjpeg($this->folder . $theme[0]) : imagecreatefromgif($this->folder . $theme[0]);
            $bg_width  = imagesx($img_bg);
            $bg_height = imagesy($img_bg);

            $img_org = ((function_exists('imagecreatetruecolor')) && PHP_VERSION >= '4.3') ?
                imagecreatetruecolor($this->width, $this->height) : imagecreate($this->width, $this->height);

            /* 将背景图象复制原始图象并调整大小 */
            if (function_exists('imagecopyresampled') && PHP_VERSION >= '4.3') // GD 2.x
            {
                imagecopyresampled($img_org, $img_bg, 0, 0, 0, 0, $this->width, $this->height, $bg_width, $bg_height);
            } else // GD 1.x
            {
                imagecopyresized($img_org, $img_bg, 0, 0, 0, 0, $this->width, $this->height, $bg_width, $bg_height);
            }
            imagedestroy($img_bg);

            $clr = imagecolorallocate($img_org, $theme[1], $theme[2], $theme[3]);

            /* 绘制边框 */
            //imagerectangle($img_org, 0, 0, $this->width - 1, $this->height - 1, $clr);

            /* 获得验证码的高度和宽度 */
            $x = ($this->width - (imagefontwidth(5) * $this->length)) / 2;
            $y = ($this->height - imagefontheight(5)) / 2;
            imagestring($img_org, 5, $x, $y, $word, $clr);

            //header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');

            // HTTP/1.1
            header('Cache-Control: private, no-store, no-cache, must-revalidate');
            header('Cache-Control: post-check=0, pre-check=0, max-age=0', false);

            // HTTP/1.0
            header('Pragma: no-cache');
            if ($this->img_type == 'jpeg' && function_exists('imagecreatefromjpeg')) {
                header('Content-type: image/jpeg');
                imageinterlace($img_org, 1);
                imagejpeg($img_org, false, 95);
            } else {
                header('Content-type: image/png');
                imagepng($img_org);
            }

            imagedestroy($img_org);

            return true;
        }
    }
}